package com.ruoyi.laiwu.controller;


import cn.dev33.satoken.annotation.SaIgnore;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.laiwu.domain.UmEventLwq;
import com.ruoyi.laiwu.mapper.UmEventLwqMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("umservice")
@SaIgnore
@Slf4j
@RequiredArgsConstructor
public class UmController {

    private final UmEventLwqMapper umEventLwqMapper;

    public R query(){
        Long l = umEventLwqMapper.selectCount(new QueryWrapper<>());
        return R.ok();
    }


    @GetMapping("eventDistribution")
    public R eventDistribution(){
        QueryWrapper<UmEventLwq> umEventLwqQueryWrapper = new QueryWrapper<>();
        umEventLwqQueryWrapper.select("area_name name","count(1) value");
        umEventLwqQueryWrapper.groupBy("area_name");
        List<Map<String, Object>> maps = umEventLwqMapper.selectMaps(umEventLwqQueryWrapper);
        maps = maps.stream().peek(i -> {
            Object areaName = i.get("name");
            i.put("name", areaName.toString().substring(areaName.toString().lastIndexOf("-")+1));
        }).collect(Collectors.toList());
        return R.ok(maps);
    }


    @GetMapping("highEvent")
    public R highEvent(){
        QueryWrapper<UmEventLwq> umEventLwqQueryWrapper = new QueryWrapper<>();
        umEventLwqQueryWrapper.select("case_class_name name","count(1) value");
        umEventLwqQueryWrapper.groupBy("case_class_name");
        umEventLwqQueryWrapper.orderByDesc("value");
        List<Map<String, Object>> maps = umEventLwqMapper.selectMaps(umEventLwqQueryWrapper);
        maps = maps.stream().peek(i -> {
            Object areaName = i.get("name");
            i.put("name", areaName.toString().substring(areaName.toString().lastIndexOf("-")+1));
        }).collect(Collectors.toList());
        return R.ok(maps);
    }




}
